package com.briup.myblog.service.impl;

import com.briup.myblog.bean.basic.Article;
import com.briup.myblog.bean.basic.Comment;
import com.briup.myblog.bean.ext.ArticleExt;
import com.briup.myblog.bean.ext.CommentExt;
import com.briup.myblog.dao.basic.ArticleMapper;
import com.briup.myblog.dao.basic.BaseUserMapper;
import com.briup.myblog.dao.basic.CategoryMapper;
import com.briup.myblog.dao.basic.CommentMapper;
import com.briup.myblog.dao.ext.ArticleExtMapper;
import com.briup.myblog.dao.ext.CommentExtMapper;
import com.briup.myblog.exception.CustomerException;
import com.briup.myblog.service.IArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j

public class ArticleServiceImpl implements IArticleService {
    @DateTimeFormat(pattern = "yyyy-mm-dd HH:mm:ss")
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleExtMapper articleExtMapper;
    @Autowired
    private CommentExtMapper commentExtMapper;
    @Autowired
    private CommentServiceImpl commentService;

    //修改资讯状态
    @Override
    public String changeArticlStatus(Integer article_id, Integer article_status) {
        Article article = articleMapper.selectByPrimaryKey(article_id);
        if (article != null) {
            articleExtMapper.updateByIdAndStatus(article_id, article_status);
            Article articleAfter = articleMapper.selectByPrimaryKey(article_id);
            //使用switc返回修改后的状态返回相应的语句,0-待审核，1-不通过，2-通过，3-推荐
            switch (articleAfter.getArticleStatus()) {
                case 0:
                    return "待审核";
                case 1:
                    return "不通过";
                case 2:
                    return "通过";
                case 3:
                    return "推荐";
                default:
                    return null;
            }

        } else {
            throw new CustomerException("资讯不存在");
        }
    }

    //根据id删除
    @Override
    public void deleteArticleById(Integer article_id) {
        //存在资讯，先删除评论，再删除资讯
        List<CommentExt> numFindCommentList = commentExtMapper.selectByArticleId(article_id);
        //存在评论
        if (numFindCommentList.size() != 0) {
            for (int i = 0; i < numFindCommentList.size(); i++) {
                //根据评论id转到评论实现类中的根据id删除
                commentService.deleteById(numFindCommentList.get(i).getCommentId());
            }
        }
        int numberArticle = articleMapper.deleteByPrimaryKey(article_id);
        if (numberArticle == 0) {
            throw new CustomerException("资讯删除失败");
        }
    }

    //根据id联合查询
    @Override
    public ArticleExt findArticleJointById(Integer article_id) {
        ArticleExt articleExt = articleExtMapper.selectArticleJointById(article_id);
        if (articleExt != null) {
            //查看详情成功，阅读次数加一
            articleExtMapper.updateReadtimesById(article_id, articleExt.getArticleReadTimes() + 1);
            return articleExt;
        } else {
            throw new CustomerException("资讯不存在");
        }
    }

    //根据状态查询
    @Override
    public PageInfo<ArticleExt> findArticleByStatus(Integer pageNum, Integer pageSize) {
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize, true);
        //联合查找所有状态为推荐的资讯的相关信息,3是推荐
        List<ArticleExt> list = articleExtMapper.selectArticleByStatus(3);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<ArticleExt> changeFind(String article_title, Integer category_id, Integer article_status, Integer pageNum, Integer pageSize) {
        //联合查询所有的资讯信息,只有分页信息
        if (article_title == "" && category_id == null && article_status == null) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByPage();
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，状态、分页信息
        else if (article_title == "" && category_id == null && !(Objects.equals(article_status, "null"))) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByStatusAndPage(article_status);
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，类别id、分页信息
        else if (article_title == "" && category_id != null && article_status == null) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByCategoryAndPage(category_id);
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，标题、分页信息
        else if (article_title != "" && category_id == null && article_status == null) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByTitleAndPage(article_title);
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，标题、类别id、分页信息
        else if (article_title != "" && !(Objects.equals(category_id, "null")) && article_status == null) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByTitleAndCategory(article_title, category_id);
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，标题、状态、分页信息
        else if (article_title != "" && category_id == null && !(Objects.equals(article_status, "null"))) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByTitleAndStatus(article_title, article_status);
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，状态、类别id、分页信息
        else if (article_title == "" && !(Objects.equals(category_id, "null")) && !(Objects.equals(article_status, "null"))) {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByCategoryAndStatus(category_id,article_status);
            return new PageInfo<>(list);
        }
        //联合查询所有的资讯信息，标题、类别id、状态、分页信息
        else {
            //设置分页信息
            PageHelper.startPage(pageNum, pageSize, true);
            List<ArticleExt> list = articleExtMapper.selectAllArticleByAll(article_title, category_id, article_status);
            return new PageInfo<>(list);
        }
    }

    @Override
    public String changeInsertOrUpdate(Article article) {
        int num;
        //拿到当前的系统时间设置给articlePublishTime
        Date date = new Date();
        article.setArticlePublishTime(date); //当前时间
        String articleid = String.valueOf(article.getArticleId());
        //没有id则为插入信息（发布）
        if (Objects.equals(articleid, "null")) {
            //当新发布资讯时状态默认设置为待审核
            article.setArticleStatus(0);
            //阅读次数为0
            article.setArticleReadTimes(0);
            //点赞次数为0
            article.setArticleThumpUp(0);
            num = articleMapper.insert(article);
            if (num != 0) {
                return "发布信息成功";
            } else {
                throw new CustomerException("发布信息失败");
            }
        } else {
            //有id为更新信息
            Article articleUpdate = articleMapper.selectByPrimaryKey(article.getArticleId());
            if (articleUpdate != null) {
                num = articleMapper.updateByPrimaryKey(article);
                if (num != 0) {
                    return "更新信息成功";
                } else {
                    throw new CustomerException("更新信息失败");
                }
            } else {
                throw new CustomerException("没有该资讯信息");
            }
        }
    }

}
